package com.yanxin.credit.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanxin.credit.dto.PageDataInfo;
import com.yanxin.credit.entity.Goods;
import com.yanxin.credit.enums.MinioBucketEnum;
import com.yanxin.credit.mapper.GoodsMapper;
import com.yanxin.credit.service.IGoodsService;
import com.yanxin.credit.service.MinIoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program spring-cloud-example
 * @description: 商品服务实现类
 * @remark: code with copilot
 * @create: 2022-02-08 18:34
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    private GoodsMapper goodsMapper;

    private MinIoService minIoService;

    public GoodsServiceImpl(GoodsMapper goodsMapper, MinIoService minIoService) {
        this.minIoService = minIoService;
        this.goodsMapper = goodsMapper;
    }

    @Override
    public PageDataInfo listBySelective(String name, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize, true);
        QueryWrapper<Goods> query = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(name)) {
            query.eq("name", name);
        }
        query.orderByDesc("create_time");
        List<Goods> goods = goodsMapper.selectList(query);
        PageDataInfo pageDataInfo = new PageDataInfo(goods);
        List<Goods> covertData = goods.stream().map(it -> {
                    it.setImageUrl(minIoService.getObjectUrl(MinioBucketEnum.CREDIT.getValue(), it.getObjectName(), 200));
                    return it;
                })
                .collect(Collectors.toList());
        pageDataInfo.setRows(covertData);
        return pageDataInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertGoods(Goods goods) {

        if (ObjectUtil.isEmpty(goods.getGoodsId())) {
            QueryWrapper<Goods> query = new QueryWrapper<>();
            query.eq("name", goods.getName());
            List<Goods> listGoods = goodsMapper.selectList(query);
            if (CollUtil.isNotEmpty(listGoods)) {
                throw new IllegalArgumentException("该商品已经存在,无须重复添加");
            }
            goods.setCreateTime(new Date());
            goods.setUpdateTime(new Date());
            goods.setCredit(goods.getCredit());
            goods.setImageUrl("");
            goods.setCreateUser(1);
            goods.setUpdateUser(1);
            return goodsMapper.insert(goods);
        } else {
            goods.setUpdateTime(new Date());
            goods.setImageUrl("");
            return goodsMapper.updateById(goods);

        }
    }

    @Override
    public int deleteGoods(long goodsId) {
        return goodsMapper.deleteById(goodsId);
    }

}
